package com.langxin.erp.util.web;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.lang3.time.DateFormatUtils;

import com.langxin.erp.util.constant.DateEnum;

/**
 * 日期工具类
 * 
 * @author 仝玉甫
 * 
 */
public final class DateUtils {

	private static final Map<DateEnum, DateFormat> formats = new HashMap<DateEnum, DateFormat>();

	static {
		formats.put(DateEnum.DATE,
				new SimpleDateFormat(DateEnum.DATE.getValue()));
		formats.put(DateEnum.UNSIGNED_DATE, new SimpleDateFormat(
				DateEnum.UNSIGNED_DATE.getValue()));
		formats.put(DateEnum.TIME,
				new SimpleDateFormat(DateEnum.TIME.getValue()));
		formats.put(DateEnum.DATETIME,
				new SimpleDateFormat(DateEnum.DATETIME.getValue()));
		formats.put(DateEnum.DIR_DATE,
				new SimpleDateFormat(DateEnum.DIR_DATE.getValue()));
		formats.put(DateEnum.NON_SEP_TIME, new SimpleDateFormat(
				DateEnum.NON_SEP_TIME.getValue()));
		formats.put(DateEnum.UNSIGNED_DATETIME, new SimpleDateFormat(
				DateEnum.UNSIGNED_DATETIME.getValue()));
		formats.put(DateEnum.DATETIMEUTC, new SimpleDateFormat(
				DateEnum.DATETIMEUTC.getValue()));
	}

	/** 获取当前时间 yyyy-MM-dd HH:mm:ss */
	public static String getNowStr(DateEnum format) {
		return formats.get(format).format(new Date());
	}

	/** 获取当前时间 */
	public static Date getNow(DateEnum format) {
		return parseDate(getNowStr(format), format);
	}

	/** 转换字符串日期 */
	public static Date parseDate(String date, DateEnum format) {
		try {
			return formats.get(format).parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	/** 转换字符串日期 */
	public static String parseDateToLoacle(String date, DateEnum format) {
		try {
			DateFormat utcFormater = formats.get(format);
			utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
			DateFormat localFormater = formats.get(DateEnum.DATETIME);
			Date gpsUTCDate = null;
			String gpsUTCDateStr = null;
			try {
				gpsUTCDate = utcFormater.parse(date);
				gpsUTCDateStr = localFormater.format(gpsUTCDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			return gpsUTCDateStr;
		} catch (Exception e) {
			return null;
		}
	}

	/** 日期转换成字符串 */
	public static String parseDate(Date date, DateEnum format) {
		return formats.get(format).format(date);
	}

	/** 日期转换成字符串 */
	public static String parseDate(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	/** 日期转换成字符串, 零时区 */
	public static String parseUTCDate(Date date, DateEnum format) {
		SimpleDateFormat df = new SimpleDateFormat(format.getValue());
		df.setTimeZone(TimeZone.getTimeZone("UTC"));
		return df.format(date);
	}

	/** Date日期格式化 */
	public static Date parseToDate(Date date, DateEnum format) {
		return parseDate(parseDate(date, format), format);
	}

	public static void main(String[] args) {
		System.out.println(parseToDate(new Date(), DateEnum.DATETIMEUTC));
	}

	/** 获取前/后日期 */
	public static String getBeforeDay(DateEnum format, int day) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, day);
		return formats.get(format).format(cal.getTime());
	}

	/**
	 * 获取前/后日期
	 * @param date  
	 * @param format
	 * @param day
	 * @return
	 */
	public static String getBeforeDay(Date date, DateEnum format, int day){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, day);
		return formats.get(format).format(cal.getTime());
	}
	
	public static String getBeforeDay(Date date, DateEnum format,int field, int day){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, day);
		return formats.get(format).format(cal.getTime());
	}
	/**
	 * 获取前/后日期 时间
	 * 
	 * @param format
	 *            格式
	 * @param day
	 *            前/后日期
	 * @param time
	 *            时间
	 * @return
	 */
	public static String getBeforeDay(DateEnum format, int day, int hour,
			int mimute, int sec) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, day);
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, mimute);
		cal.set(Calendar.SECOND, sec);
		return formats.get(format).format(cal.getTime());
	}

	/**
	 * 得到不要下划线的时间格式
	 * 
	 * @return
	 */
	public static String getFormattedLongMillTimestamp() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}
	
	/**
	 * 两个时间相减，算出秒数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long minusMillTimes(Date date1, Date date2){
		date1 = parseToDate(date1, DateEnum.DATETIME);
		date2 = parseToDate(date2, DateEnum.DATETIME);
		return (date1.getTime() - date2.getTime())/1000;
	}

	
	/**
	 * 不足10，补0
	 * @param time
	 * @return
	 */
	public static String parse(int time){
		if(time < 10){
			return "0" + time;
		}else{
			return time + "";
		}
	}
	
	/**
     * 获取指定时间的开始时间
     * @author Administrator
     * @date   2015年12月17日
     * @param date
     * @return
     */
    public static Date getDateStart(Date date){
    	Calendar c = Calendar.getInstance();
    	c.setTimeInMillis(date.getTime());
    	c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 0, 0, 0);
    	return parseDate(DateFormatUtils.format(c, "yyyy-MM-dd HH:mm:ss"),DateEnum.DATETIME);
    }
    
    /**
     * 获取指定时间的结束时间
     * @author Administrator
     * @date   2015年12月17日
     * @param date
     * @return
     */
    public static Date getDateEnd(Date date){
    	Calendar c = Calendar.getInstance();
    	c.setTimeInMillis(date.getTime());
    	c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 23, 59, 59);
    	return parseDate(DateFormatUtils.format(c, "yyyy-MM-dd HH:mm:ss"),DateEnum.DATETIME);
    }
}
